#Goal: conversion of spss community-nr files to .dta sets

#install required packages if you do not already have them

list.of.packages <- c("haven", "foreign", "readstata13", "tidyverse")
new.packages <- list.of.packages[!(list.of.packages %in% installed.packages()[,"Package"])]
if(length(new.packages)) install.packages(new.packages)

#load libraries
library(haven)
library(foreign)
library(readstata13)
library(tidyverse)

#INSERT YOUR PATH HERE TO SHP_VOX_prep folder
setwd("")
#wave 1999
waves <- 99
for (w in waves)  {
  data <-read_sav(paste0("SHP/community_numbers_99_17/spss/communes",w,".sav"))
  #change variable labelling
  names(data)[1] <- paste0("idhouse")
  #label communitynumber (BFS)
  names(data)[2] <- paste0("bfsnr")
  data$year <- 1999
  data$shpwave <- 1
  save.dta13(data, paste0("SHP/community_numbers_99_17/stata/communes",w,".dta")) 
}
###########################################################################################################################################################
#waves 2000
waves <- "00"
for (w in waves)  {
  data <-read_sav(paste0("SHP/community_numbers_99_17/spss/communes",w,".sav"))
  #change variable labelling
  names(data)[1] <- paste0("idhouse")
  #label communitynumber (BFS)
  names(data)[2] <- paste0("bfsnr")
  data$year <- 2000
  data$shpwave <- 2
  save.dta13(data, paste0("SHP/community_numbers_99_17/stata/communes",w,".dta")) 
}
###########################################################################################################################################################

#wave 2001-2009
waves <- c(paste0("0", seq(0:8)))
for (w in waves)  {
  data <-read_sav(paste0("SHP/community_numbers_99_17/spss/communes",w,".sav"))
  #change variable labelling
  names(data)[1] <- paste0("idhouse")
  #label communitynumber (BFS)
  names(data)[2] <- paste0("bfsnr")
  data$year <- 2000 + as.numeric(w)
  data$shpwave <- as.numeric(w) + 2
  save.dta13(data, paste0("SHP/community_numbers_99_17/stata/communes",w,".dta")) 
}


###########################################################################################################################################################
#waves 2010
waves <- 10
for (w in waves)  {
  data <-read_sav(paste0("SHP/community_numbers_99_17/spss/communes",w,".sav"))
  #change variable labelling
  names(data)[1] <- paste0("idhouse")
  #label communitynumber (BFS)
  names(data)[2] <- paste0("bfsnr")
  data$year <- 2010
  data$shpwave <- 12
  save.dta13(data, paste0("SHP/community_numbers_99_17/stata/communes",w,".dta")) 
}
###########################################################################################################################################################
#waves 2011 - 2017
waves <- c(paste0("1", seq(0:6)))
for (w in waves) {
  data <-read_sav(paste0("SHP/community_numbers_99_17/spss/communes",w,".sav"))
  #change variable labelling
  names(data)[1] <- paste0("idhouse")
  #label communitynumber (BFS)
  names(data)[2] <- paste0("bfsnr")
  data$year <- 2000 + as.numeric(w)
  data$shpwave <- as.numeric(w) + 2
  save.dta13(data, paste0("SHP/community_numbers_99_17/stata/communes",w,".dta")) 
}




